package controller.binder;

import java.beans.PropertyEditorSupport;
import java.util.ArrayList;
import java.util.List;

import cultu.customer.message.Preference;

/**
 * Preferences list property editor.
 * @author Marc-Antoine
 *
 */
public class PreferenceListPropertyEditor extends PropertyEditorSupport
{
  /**
   * {@inheritDoc}
   */
  @Override
  public void setAsText(final String text) {
  
    // The unserialized preferences list.
    final List<Preference> preferencesList = new ArrayList<Preference>();
  
    // Text check.
    if (text != null && !"".equals(text)) {
      
      // Split text.
      final String[] preferencesStringArray = text.split(":");
      
      // Loop over preferences string to unseralize.
      for (String prefenceString : preferencesStringArray) {
        
        // Create preference and set id.
        final Preference preference = new Preference();
        preference.setHjid(Long.valueOf(prefenceString));
        
        // Add to list.
        preferencesList.add(preference);
      }
      
    }
    
    setValue(preferencesList);
  }
}
